﻿namespace Core
{
    using System;
    /// <summary>
    /// 依赖解析工厂类
    /// </summary>
    public class DependencyResolverFactory : IDependencyResolverFactory
    {
        private readonly Type _resolverType;
        public DependencyResolverFactory(string resolverTypeName)
        {
            Check.Argument.IsNotEmpty(resolverTypeName, "resolverTypeName");
            //GetType(获取具有指定名称的 System.Type，指定是否执行区分大小写的搜索，以及在找不到类型时是否引发异常。)
            _resolverType = Type.GetType(resolverTypeName, true, true);
        }
        /// <summary>
        /// 通过配置获得依赖解析类型名称
        /// </summary>
        public DependencyResolverFactory() : this(new ConfigurationManagerWrapper().AppSettings["dependencyResolverTypeName"])
        {
        }
        /// <summary>
        /// 根据名称创建一个依赖解析类实例
        /// </summary>
        /// <returns></returns>
        public IDependencyResolver CreateInstance()
        {
            return Activator.CreateInstance(_resolverType) as IDependencyResolver;
        }
    }
}